package sz.szsmk.citizencard.ui.newversion.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.vlayout.DelegateAdapter
import com.alibaba.android.vlayout.VirtualLayoutManager
import com.alibaba.android.vlayout.layout.OnePlusNLayoutHelper
import com.gyf.immersionbar.ImmersionBar
import com.youth.banner.indicator.CircleIndicator
import kotlinx.android.synthetic.main.fragment_life.*
import kotlinx.android.synthetic.main.include_cultureconsume.*
import kotlinx.android.synthetic.main.include_discount.*
import kotlinx.android.synthetic.main.include_happyplay.*
import kotlinx.android.synthetic.main.include_servicenetwork.*
import kotlinx.android.synthetic.main.layout_banner.*
import sz.szsmk.citizencard.DaggerApplication
import sz.szsmk.citizencard.R
import sz.szsmk.citizencard.model.network.bean.response.BaseBean
import sz.szsmk.citizencard.model.network.bean.response.PrepayResponse
import sz.szsmk.citizencard.model.network.clarkbean.response.AdInfo
import sz.szsmk.citizencard.model.network.clarkbean.response.AreaInfo
import sz.szsmk.citizencard.model.network.clarkbean.response.AreaInfoResponse
import sz.szsmk.citizencard.mvp.BasePresenter
import sz.szsmk.citizencard.ui.activity.IMainActivity
import sz.szsmk.citizencard.ui.adapter.AllTypeAdapter
import sz.szsmk.citizencard.ui.dzsmkfragment.AppBaseFragment
import sz.szsmk.citizencard.ui.newversion.fragment.adapter.*
import sz.szsmk.citizencard.ui.newversion.fragment.decoration.RecycleViewDivider
import sz.szsmk.citizencard.ui.newversion.fragment.presenter.LifePresenter
import sz.szsmk.citizencard.ui.newversion.fragment.view.LifeMvpView
import sz.szsmk.citizencard.util.Constant
import sz.szsmk.citizencard.util.JumpThirdView
import sz.szsmk.citizencard.util.JumpUtils
import javax.inject.Inject

/**
 *  author : hyt
 *  date : 2020/7/2
 *  description :
 */
class LifeFragment : AppBaseFragment(),  LifeMvpView {

    //banner
    var datas = ArrayList<AdInfo>()
    lateinit var bannerAdapter : HomeBannerAdapter

    //爆款来袭
    //文化
    lateinit var onePlusNhotComeingLayoutHelper : OnePlusNLayoutHelper
    lateinit var onePlusTwoAdatper :OnePlusTwoAdapter

    //优惠购
    var discountAdapter :LifeDiscountAdapter ?= null

    //快乐玩
    var happyPlayAdapter:LifeHappyPlayAdapter?=null

    //文旅消费
    var cultureConsumeAdapter :CultureConsumeAdapter?=null

    //服务站点
    var serviceNetworkAdapter : AllTypeAdapter?=null

    @Inject
    lateinit var presenter: LifePresenter

    var root:View?=null

    override fun initPresenters(): Array<BasePresenter<*>>? {
        return arrayOf(presenter)
    }
    companion object{
        fun getInstance(): LifeFragment? {
            return LifeFragment()
        }
    }

    override fun onCreateView(
            inflater: LayoutInflater,
            container: ViewGroup?,
            savedInstanceState: Bundle?
    ): View? {
        if(root==null){
            root = inflater.inflate(R.layout.fragment_life, null)
            component.inject(this)
        }
        isViewOK = true
        return root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        //MainActivity沉浸  手动改变view的高度  便于 滑动整体 标题栏变色
        var layoutParams = v_statusBar.layoutParams as ViewGroup.MarginLayoutParams
        layoutParams.height = ImmersionBar.getStatusBarHeight(requireActivity())
        v_statusBar.layoutParams = layoutParams
    }

    private var isViewOK = false //是否完成 View 初始化
    private var isFirst = true //是否为第一次加载
    override fun onResume() {
        super.onResume()
        if (isViewOK && isFirst) {
            //加载数据时判断是否完成view的初始化，以及是不是第一次加载此数据
            loadDate()
            //加载第一次数据后改变状态，后续不再重复加载
            isFirst = false
        }

    }

    override fun getTopTitle() = null

    private fun loadDate() {
        //banner 初始化
        bvp_banner.apply {
            //指示器
            indicator =  CircleIndicator(view?.context)
            //设置圆角
            setBannerRound(16f)
            bannerAdapter = HomeBannerAdapter(context, datas)
            adapter = bannerAdapter
        }

        //爆款来袭
        val virtualLayoutManager = VirtualLayoutManager(requireContext())
        val delegateAdapter = DelegateAdapter(virtualLayoutManager, true)
        val adapters = mutableListOf<DelegateAdapter.Adapter<*>>()
        onePlusNhotComeingLayoutHelper = OnePlusNLayoutHelper()
        onePlusTwoAdatper = OnePlusTwoAdapter(requireContext(), onePlusNhotComeingLayoutHelper)
        adapters.add(onePlusTwoAdatper)
        delegateAdapter.addAdapters(adapters)
        rv_hotComeing.layoutManager = virtualLayoutManager
        rv_hotComeing.adapter = delegateAdapter

        //优惠购
        discountAdapter = LifeDiscountAdapter(context)
        rv_discount.layoutManager = GridLayoutManager(context, 3)
        rv_discount.addItemDecoration(SpaceItemDecoration(10))
        rv_discount.adapter = discountAdapter

        //快乐玩
        happyPlayAdapter = LifeHappyPlayAdapter(context)
        rv_happyPlay.layoutManager = LinearLayoutManager(context)
        rv_happyPlay.addItemDecoration(RecycleViewDivider(requireContext(), LinearLayoutManager.HORIZONTAL))
        rv_happyPlay.adapter = happyPlayAdapter

        //文旅消费
        cultureConsumeAdapter = CultureConsumeAdapter(context)
        rv_cultureConsume.layoutManager = GridLayoutManager(context, 3)
        rv_cultureConsume.addItemDecoration(SpaceItemDecoration(10))
        rv_cultureConsume.adapter =  cultureConsumeAdapter

        //服务网点
        serviceNetworkAdapter = AllTypeAdapter(context, "life")
        rv_serviceNet.layoutManager = LinearLayoutManager(context)
        rv_serviceNet.adapter = serviceNetworkAdapter

        srl_refresh.setEnableLoadMore(false)
        //下拉刷新
        srl_refresh.setOnRefreshListener {
            requestData()
        }

        requestData()
    }

    private fun requestData(){
        //首页模块数据
        presenter.queryAreaInfo()
    }

    override fun getAreaInfoSuccess(response: AreaInfoResponse) {
        srl_refresh.finishRefresh()
        for(areaBean in response.areaInfoVoList) {
            when (areaBean.resource_area_id) {
                "206" -> {
                    //生活头部banner
                    lifeBanner(areaBean)
                }
                "207" -> {
                    //爆款来袭
                    hotComing(areaBean)
                }
                "208" -> {
                    //优惠购
                    discount(areaBean)
                }
                "209" -> {
                    //快乐玩
                    happyPlay(areaBean)
                }
                "210" -> {
                    //文旅消费
                    cultureConsume(areaBean)
                }
            }
        }
    }

    //文旅消费
    private fun cultureConsume(areaBean: AreaInfo?) {
        if(areaBean?.adInfoVoList?.isNotEmpty()!!){
            tv_cultureConsumeTitle.text = areaBean.resource_area_name
            tv_cultureConsumeSubTitle.text = areaBean.resource_sub_title
            cultureConsumeAdapter?.addDatas(areaBean.adInfoVoList)
            tv_cultureConsumeSubTitle.setOnClickListener {
                JumpThirdView.baseToJumpView(requireContext(), areaBean.title_url, areaBean.sys_label, areaBean.need_header, "")
            }
        }else{
            include_cultureConsume.visibility = View.GONE
        }
    }

    //服务网点
    override fun getWebsiteListSuccess(response: PrepayResponse) {
        serviceNetworkAdapter?.setTypeData(response.websiteListInfo, 0) //0的话是刷新，1是增加
        tv_serviceNetSubTitle.setOnClickListener {
            //服务网点
            JumpUtils.jump(requireContext(), "nerwork", 0)
        }
    }

    //快乐玩
    private fun happyPlay(areaBean: AreaInfo?) {
        if(areaBean?.adInfoVoList?.isNotEmpty()!!){
            tv_happyPlayTitle.text = areaBean.resource_area_name
            tv_happyPlaySubTitle.text = areaBean.resource_sub_title
            happyPlayAdapter?.addDatas(areaBean.adInfoVoList)

            tv_happyPlaySubTitle.setOnClickListener {
                val bundle = Bundle()
                bundle.putString("url", Constant.BASE_URL + "app-front-end/#/live/play")
                JumpUtils.jumptoNatvie(requireContext(), "webView", 1, bundle)
            }
        }else{
            include_discount.visibility = View.GONE
        }
    }

    //优惠购
    private fun discount(areaBean: AreaInfo?) {
        if(areaBean?.adInfoVoList?.isNotEmpty()!!){
            tv_discountTitle.text = areaBean.resource_area_name
            tv_discountSubTitle.text = areaBean.resource_sub_title
            discountAdapter?.addDatas(areaBean.adInfoVoList)

            tv_discountSubTitle.setOnClickListener {
                JumpThirdView.baseToJumpView(requireContext(), areaBean.title_url, areaBean.sys_label, areaBean.need_header, "")
            }
        }else{
            include_discount.visibility = View.GONE
        }
    }

    //生活 爆款来袭
    private fun hotComing(areaBean: AreaInfo?) {
        if(areaBean?.adInfoVoList?.isNotEmpty()!!){
            //标题
            tv_hotComeing.text = areaBean.resource_area_name
            //右边 文字
            onePlusTwoAdatper.addDatas(areaBean.adInfoVoList)
        }else{
//            cv_cultureTour.visibility = View.GONE
        }
    }

    //生活顶部banner
    private fun lifeBanner(areaBean: AreaInfo?) {
        if(areaBean?.adInfoVoList?.isNotEmpty()!!){
            bannerAdapter.setDatas(areaBean.adInfoVoList)
            bannerAdapter.notifyDataSetChanged()
        }else{
//            include_banner.visibility = View.GONE
        }
    }


    override fun onFail(baseBean: BaseBean) {
    }

}
